<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="sys" tagdir="/WEB-INF/tags/sys"%>
<%@taglib uri="/WEB-INF/tlds/webui.tld" prefix="webui"%>
<script type="text/javascript" src="${contextPath}/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="${contextPath}/js/jquery.ztree.excheck-3.5.js"></script>
<webui:enum var="typeEnum" code="ValueEnum"/>
<webui:panel>
		<div class="col-md-3" style="height: 700px;overflow: scroll;overflow-x:hidden;">
			<div id="enumDataTree"></div>
		</div>
		<div class="col-md-9">
			<div class="panel panel-default" id="database">
				<div class="panel-heading">
				    <h3 class="panel-title">数据信息</h3>
				</div>
			    <div class="panel-body">
		        	<webui:form method="get" styleClass="form-horizontal" id="editFormEnumDataForm">
						<input hidden="hidden" value="" name="id" id="editFormEnumDataId" />
						<webui:input label="数据名称" name="name" id="editFormEnumDataName" styleClass="col-sm-6" labelClass="col-sm-6" inputClass="col-sm-6" 
						 type="text">
							<webui:validator required="true" maxlength="128">
								{maxlength:"数据名称太长！"}
							</webui:validator>
						</webui:input>
						<webui:input label="数据编码" name="code" id="editFormEnumDataCode" styleClass="col-sm-6" labelClass="col-sm-6" inputClass="col-sm-6" 
						 type="text" >
							<webui:validator maxlength="64">
								{maxlength:"数据编码太长！"}
							</webui:validator>
						</webui:input>
						<webui:input label="数据值" name="value" id="editFormEnumDataValue" styleClass="col-sm-6" labelClass="col-sm-6" inputClass="col-sm-6" 
						 type="text" >
							<webui:validator maxlength="64">
								{maxlength:"数据值太长！"}
							</webui:validator>
						</webui:input>
						<webui:select label="类型" name="type" id="editFormEnumDataType" styleClass="col-sm-6" 
						 labelClass="col-sm-6" inputClass="col-sm-6" 
						 lookup="${typeEnum}" multiple="false">
						 	<webui:validator required="true"/>
						</webui:select>
					</webui:form>
			    </div>
			    <div class="panel-footer">
			    	<div style="margin-left: 800px;">
				    	<webui:button id="baseEnumDataSaveBtn" type="button" buttonClass="btn-primary" ladda="true" onclick="saveBaseEnumData()">保存</webui:button>
			    	</div>
				</div>
			</div>
			<div class="panel panel-default">
				<div class="panel-heading">
				    <h3 class="panel-title">子数据列表</h3>
				</div>
			    <div class="panel-body">
			      	<div class="col-md-9" style="height: 370px;width:100%;overflow-y: auto;overflow-x:hidden;">
						<button class="btn btn-primary" data-toggle="modal" data-target="#enumDataModal" style="margin-left: 20px;height: 25px;padding: 3px;">新建</button>
						<webui:table id="tb_enumDatas"
							datas="_embedded.enumDatas"
							dataUrl="${contextPath}/rest/enumDatas/search/filter?page=0&size=5">
							<webui:column field="id" editor="text" styleClass="display-none">{{row.id}}</webui:column>
							<webui:column field="parentId" editor="text" styleClass="display-none">{{row.parentId}}</webui:column>
							<webui:column field="name" editor="text" title="数据名称" style="width:50px;">{{row.name}}
								<webui:validator required="true" maxlength="128">
									{maxlength:"数据名称太长！"}
								</webui:validator>
							</webui:column>
							<webui:column field="code" editor="text" title="数据编码" style="width:200px;">{{row.code}}
								<webui:validator maxlength="64">
									{maxlength:"数据编码太长！"}
								</webui:validator>
							</webui:column>
							<webui:column field="value" editor="text" title="数据值" style="width:200px;">{{row.value}}
								<webui:validator maxlength="64">
									{maxlength:"数据值太长！"}
								</webui:validator>
							</webui:column>
							<webui:column field="type" editor="text" title="值类型" style="width:50px;" lookup="${typeEnum}">{{row.typeDesc}}
								<webui:validator />
							</webui:column>
							<webui:column title="操作" editor="opt" style="width:80px;">
								<button class="btn btn-xs btn-primary"  v-bind:onclick="'javascript:editEnumData('+row.id+',\''+row.name+'\')'" type="button">修改</button>
								<button class="btn btn-xs btn-danger"  v-bind:onclick="'javascript:deleteEnumData('+row.id+',\''+row.name+'\')'" type="button">删除</button>
							</webui:column>
						</webui:table>
					</div>
			    </div>
			    <div class="panel-footer">
				</div>
			</div>
		</div>
</webui:panel>
<div class="modal fade" id="enumDataModal" tabindex="-1" role="dialog" style="height:600px;width:600px;margin-left: 400px;overflow: hidden;" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
				<h4 class="modal-title" id="myModalLabel">
					新增基础数据
				</h4>
			</div>
			<div class="modal-body" style="height: 400px;">
				<webui:form method="get" styleClass="form-horizontal" id="enumDataEditorForm">
					<input hidden="hidden" value="" name="id" id="addFormEnumDataId" />
					<input hidden="hidden" value="" name="parentId" id="addFormEnumDataParentId" />
					<webui:input label="数据名称" name="name" id="addFormEnumDataName" labelClass="col-sm-4" inputClass="col-sm-5" 
					 type="text">
						<webui:validator required="true" maxlength="128">
							{maxlength:"数据名称太长！"}
						</webui:validator>
					</webui:input>
					<webui:input label="数据编码" name="code" id="addFormEnumDataCode" labelClass="col-sm-4" inputClass="col-sm-5" 
					 type="text" >
						<webui:validator maxlength="64">
							{maxlength:"数据编码太长！"}
						</webui:validator>
					</webui:input>
					<webui:input label="数据值" name="value" id="addFormEnumDataValue" labelClass="col-sm-4" inputClass="col-sm-5" 
					 type="text" >
						<webui:validator maxlength="64">
							{maxlength:"数据值太长！"}
						</webui:validator>
					</webui:input>
					<webui:select label="类型" name="type" id="addFormEnumDataType"
					 labelClass="col-sm-4" inputClass="col-sm-5" 
					 lookup="${typeEnum}" multiple="false">
					 	<webui:validator required="true"/>
					</webui:select>
				</webui:form>
				<div class="row">
					<div class="col-md-12" align="center">
						<webui:button type="button" buttonClass="btn-white" onclick="cancelEnumData()">取消</webui:button>
						<webui:button id="addEnumDataSaveBtn" type="button" buttonClass="btn-primary" ladda="true" onclick="saveEnumData()">保存</webui:button>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="modal fade" id="enumDataEditModal" tabindex="-1" role="dialog" style="height:600px;width:600px;margin-left: 400px;overflow: hidden;" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
				<h4 class="modal-title" id="myModalLabel">
					修改基础数据
				</h4>
			</div>
			<div class="modal-body" style="height: 400px;">
				<webui:form method="get" styleClass="form-horizontal" id="enumDataEditEditorForm">
					<input hidden="hidden" value="" name="id" id="editModelFormEnumDataId" />
					<input hidden="hidden" value="" name="parentId" id="editModelFormEnumDataParentId" />
					<webui:input label="数据名称" name="name" id="editModelFormEnumDataName" labelClass="col-sm-4" inputClass="col-sm-5" 
					 type="text">
						<webui:validator required="true" maxlength="128">
							{maxlength:"数据名称太长！"}
						</webui:validator>
					</webui:input>
					<webui:input label="数据编码" name="code" id="editModelFormEnumDataCode" labelClass="col-sm-4" inputClass="col-sm-5" 
					 type="text" >
						<webui:validator maxlength="64">
							{maxlength:"数据编码太长！"}
						</webui:validator>
					</webui:input>
					<webui:input label="数据值" name="value" id="editModelFormEnumDataValue" labelClass="col-sm-4" inputClass="col-sm-5" 
					 type="text" >
						<webui:validator maxlength="64">
							{maxlength:"数据值太长！"}
						</webui:validator>
					</webui:input>
					<webui:select label="类型" name="type" id="editModelFormEnumDataType"
					 labelClass="col-sm-4" inputClass="col-sm-5" 
					 lookup="${typeEnum}" multiple="false">
					 	<webui:validator required="true"/>
					</webui:select>
				</webui:form>
				<div class="row">
					<div class="col-md-12" align="center">
						<webui:button type="button" buttonClass="btn-white" onclick="cancelEditEnumData()">取消</webui:button>
						<webui:button id="configSaveBtn" type="button" buttonClass="btn-primary" ladda="true" onclick="saveEditEnumData()">保存</webui:button>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
	var enumData = null;
 	$(function () {
 		tb_enumDatas.completeEditRow = function(){
		    if(enumDataTree!=null){
			    enumDataTree.jstree(true).refresh();
			}
 		}
 		tb_enumDatas.completeAppendRow = function(){
		    if(enumDataTree!=null){
			    enumDataTree.jstree(true).refresh();
			}
 		} 		
 		
 	 	enumDataTree = $('#enumDataTree').jstree({
        	'plugins' : [ 'types', 'dnd' ],
        	'core' : {
				'data' : {
						"url" : '${contextPath}/sys/enumData/tree.action',
						"data" : function (node) {
							return { "id" : node.id };
						}
					}
			},
			'types' : {
	            'default' : {
	                'icon' : 'fa fa-folder text-navy'
	            }
	        }
		});
 	 	
 	 	$("#enumDataTree").on("loaded.jstree", function (event, data) {
            data.instance.open_node(1);
            $("#1_anchor").click();
        });
		$('#enumDataTree').on("select_node.jstree", function(e,data){
			var id = data.node.id;
			$("#editFormEnumDataId").val(id);
			getEnumData(id);
			$("#parentId_new").val(id);
			tb_enumDatas.url = "${contextPath}/rest/enumDatas/search/filter?parentId="+id;
			tb_enumDatas.reload();
	 	});
	});
 	
	function deleteEnumData(id,name){
 		confirm('确认删除基础数据?', function(){
			$.ajax({
				method:"get",
				url:'${contextPath}/sys/enumData/delete/'+id+'.action',
				success:function(data){
					if(data.result == "0"){
						showMessage('提示','删除'+name+'成功。');
						tb_enumDatas.reload();
						enumDataTree.jstree(true).refresh();
					}else{
						showError('提示',data.message);
					}
				}
			});	
        });
	} 
	
	//初始化父节点参数
	function getEnumData(id){
		$.ajax({
 	 	    type:"GET",
 	 	    async:false,
 	 	    url:"${contextPath}/rest/enumDatas/search/findById?id="+id,
 	 	    success:function(data){
 	 	    	var name = data.name;
 	 	    	var code = data.code;
 	 	    	var value = data.value;
 	 	    	var type = data.type;
 	 	    	editFormEnumDataFormValidator.resetForm();
 	 	    	$("#editFormEnumDataName").val(name);
 	 	    	$("#editFormEnumDataCode").val(code);
 	 	    	$("#editFormEnumDataValue").val(value);
 	 	    	$("#editFormEnumDataType").val(type);
 	 	    }
 	 	}); 
	}
	
	//保存父节点
	function saveBaseEnumData(){
		if(!editFormEnumDataFormValidator.form()){
			baseEnumDataSaveBtn.ladda('stop');
			return;
		}
		var id = $("#editFormEnumDataId").val();
		var name = $("#editFormEnumDataName").val();
		var code = $("#editFormEnumDataCode").val();
		var value = $("#editFormEnumDataValue").val();
		var type = $("#editFormEnumDataType").val();
		var data = {id:id,name:name,code:code,value:value,type:type};
		$.ajax({
 	 	    type:"POST",
 	 	    async:false,
 	 	    url:"${contextPath}/sys/enumData/save.action",
 	 	    contentType:"application/json; charset=utf-8",
 	 	    data:JSON.stringify(data),
 	 	    success:function(data){
 	 	    	if(data.result == "0"){
 	 	    		showMessage('提示','保存成功。');
 	 	    		enumDataTree.jstree(true).refresh();
 	 	    	}else{
 	 	    		showError('提示',data.message);
 	 	    	}
 	 	    }
 	 	}); 
	}
	
	//新增保存
	function saveEnumData(){
		if(!enumDataEditorFormValidator.form()){
			addEnumDataSaveBtn.ladda('stop');
			return;
		}
		var parentId = $("#addFormEnumDataParentId").val();
		var name = $("#addFormEnumDataName").val();
		var code = $("#addFormEnumDataCode").val();
		var value = $("#addFormEnumDataValue").val();
		var type = $("#addFormEnumDataType").val();
		var data = {parentId:parentId,name:name,code:code,value:value,type:type};
		$.ajax({
 	 	    type:"POST",
 	 	    async:false,
 	 	    url:"${contextPath}/sys/enumData/save.action",
 	 	    contentType:"application/json; charset=utf-8",
 	 	    data:JSON.stringify(data),
 	 	    success:function(data){
 	 	    	if(data.result == "0"){
 	 	    		showMessage('提示','添加成功。');
 	 	    		$('#enumDataEditorForm').reset();
 	 	    		$('#enumDataModal').modal('toggle');
 	 	    		enumDataTree.jstree(true).refresh();
 	 	    	}else{
 	 	    		showError('提示',data.message);
 	 	    	}
 	 	    }
 	 	}); 
	} 
	
	//修改保存
	function saveEditEnumData(){
		if(!enumDataEditEditorFormValidator.form()){
			shoppingCartSSaveBtn.ladda('stop');
			return;
		}
	    var id = $("#editModelFormEnumDataId").val();
		var parentId = $("#editModelFormEnumDataParentId").val();
		var name = $("#editModelFormEnumDataName").val();
		var code = $("#editModelFormEnumDataCode").val();
		var value = $("#editModelFormEnumDataValue").val();
		var type = $("#editModelFormEnumDataType").val();
		var data = {id:id,parentId:parentId,name:name,code:code,value:value,type:type};
		$.ajax({
 	 	    type:"POST",
 	 	    async:false,
 	 	    url:"${contextPath}/sys/enumData/save.action",
 	 	    contentType:"application/json; charset=utf-8",
 	 	    data:JSON.stringify(data),
 	 	    success:function(data){
 	 	    	if(data.result == "0"){
 	 	    		showMessage('提示','修改成功。');
 	 	    		$('#enumDataEditEditorForm').reset();
 	 	    		$('#enumDataEditModal').modal('toggle');
 	 	    		enumDataTree.jstree(true).refresh();
 	 	    	}else{
 	 	    		showError('提示',data.message);
 	 	    	}
 	 	    }
 	 	}); 
	} 
	
	//修改初始化数据
	function editEnumData(id,name){
		$.ajax({
 	 	    type:"GET",
 	 	    async:false,
 	 	    url:"${contextPath}/rest/enumDatas/search/findById?id="+id,
 	 	    success:function(data){
 	 	    	var parentId = data.parentId;
 	 	    	var name = data.name;
 	 	    	var code = data.code;
 	 	    	var value = data.value;
 	 	    	var type = data.type;
 	 	    	enumDataEditEditorFormValidator.resetForm();
 	 	    	$("#editModelFormEnumDataId").val(id);
 	 	    	$("#editModelFormEnumDataParentId").val(parentId);
 	 	    	$("#editModelFormEnumDataName").val(name);
 	 	    	$("#editModelFormEnumDataCode").val(code);
 	 	    	$("#editModelFormEnumDataValue").val(value);
 	 	    	$("#editModelFormEnumDataType").val(type);
 	 	    	$('#enumDataEditModal').modal('toggle');
 	 	    }
 	 	}); 
	}
	
	$(function(){
		$('#enumDataModal').on('show.bs.modal', function (){
			enumDataEditorFormValidator.resetForm();
			var parentId = $("#editFormEnumDataId").val();
			$("#addFormEnumDataParentId").val(parentId);
			$("#addFormEnumDataType").val(1);
		}) 
		$('#enumDataModal').on('hide.bs.modal', function (){
			$('#enumDataEditorForm').reset();
		}) 
		$('#editFormEnumDataType').val(1);
	});
	
	//取消新增
	function cancelEnumData(){
		$('#enumDataEditorForm').reset();
		$('#enumDataModal').modal('toggle');
	} 
	
	//取消修改
	function cancelEditEnumData(){
		$('#enumDataEditEditorForm').reset();
		$('#enumDataEditModal').modal('toggle');
	} 
</script>